package com.example.homework;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;

public class employActivity extends AppCompatActivity implements AdapterView.OnItemClickListener{
    public static final String TAG="Click";
    ListView listView;
    com.example.homework.MyAdapter adapter;
    private ProgressBar bar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_employ);
        //listView=findViewById(R.id.mylist2);
        bar =findViewById(R.id.progressBar2);
        //页面无数据时显示加载中
        //listView.setEmptyView(bar);
        //初始化数据
        ArrayList<HashMap<String,String>> listItems=new ArrayList<HashMap<String,String>>();
        SimpleAdapter listItemAdapter=new SimpleAdapter(this,
                listItems,
                R.layout.list_item,
                new String[]{"ItemTitle","ItemDate"},
                new int[]{R.id.title,R.id.date}
        );
        //绑定控件
        listView =findViewById(R.id.mylist2);
        listView.setAdapter(listItemAdapter);
        listView.setOnItemClickListener(this);

        Handler handler=new Handler(Looper.myLooper()) {
            public void handleMessage(Message msg) {
                if (msg.what == 7) {
                    ArrayList<HashMap<String, String>> list = (ArrayList<HashMap<String, String>>) msg.obj;
                    adapter=new com.example.homework.MyAdapter(employActivity.this,R.layout.list_item,list);
                    listView.setAdapter(adapter);
                    //隐藏bar
                    bar.setVisibility(View.INVISIBLE);
                }
                super.handleMessage(msg);
            }
        };
        MapTask task=new MapTask();
        task.setHandler(handler);
        Thread t= new Thread(task);
        t.start();
    }
    public void onItemClick(AdapterView<?> parent,View view,int position,long id){
        Log.i(TAG, "onItemClick: onItemClick");
        //获取数据项
        HashMap<String,String> map=(HashMap<String,String>)listView.getItemAtPosition(position);
        Log.i(TAG,"onItemClick:cname="+map.get("cname"));
        Log.i(TAG,"onItemClick:cdate="+map.get("cdate"));
        TextView title=(TextView) view.findViewById(R.id.title);
        TextView date=(TextView) view.findViewById(R.id.date);
        String title2=String.valueOf(title.getText());
        String date2=String.valueOf(date.getText());
        float date3=Float.valueOf(date2);
        Intent intent=new Intent(this,employdetailActivity.class);
        intent.putExtra("name1",title2);
        intent.putExtra("rate1",date3);
        intent.putExtra("pos",position);
        Log.i(TAG,"onItemLongClick:pos="+position);
        startActivity(intent);
    }
}